package vip.ola.pay.channel.dragonpay;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.common.util.PayDigestUtil;
import vip.ola.core.entity.PayOrder;
import vip.ola.core.entity.PayPassageAccount;
import vip.ola.pay.ctrl.common.BaseController;
import vip.ola.pay.mq.BaseNotify4MchPay;
import vip.ola.pay.service.RpcCommonService;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static vip.ola.core.common.constant.PayConstant.PAY_STATUS_COMPLETE;
import static vip.ola.core.common.constant.PayConstant.PAY_STATUS_SUCCESS;

@RequestMapping("/api/slcloud")
@Controller
public class SlCloudPayController extends BaseController {
    private static final MyLog _log = MyLog.getLog(SlCloudPayController.class);

    @Autowired
    private RpcCommonService rpcCommonService;
    @Autowired
    public BaseNotify4MchPay baseNotify4MchPay;

    @RequestMapping("/notify")
    @ResponseBody
    public String notify(@RequestParam Map<String,Object> map){
        _log.info("接收slcloud回调：" + map);
        String mchOrderNo = map.get("mchOrderNo").toString();
        PayOrder payOrder = rpcCommonService.rpcPayOrderService.findByPayOrderId(mchOrderNo);
        if(payOrder == null){
            return "order not exist!";
        }
        Long orderMoney = (Long)map.get("amount");
        if (orderMoney.longValue() != payOrder.getAmount()){
            return "money fail";
        }
        if (payOrder.getStatus() == PAY_STATUS_SUCCESS || payOrder.getStatus() == PAY_STATUS_COMPLETE) {
            return "success";
        }
        PayPassageAccount payPassageAccount = rpcCommonService.rpcPayPassageAccountService.findById(payOrder.getPassageAccountId());
        if(payPassageAccount == null || StringUtils.isBlank(payPassageAccount.getParam())){
            return "fail";
        }
        String resSign = String.valueOf(map.get("sign"));  //接口返回sign参数值

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("payOrderId", map.get("payOrderId"));
        paramsMap.put("mchId", map.get("mchId"));
        paramsMap.put("appId", map.get("appId"));
        paramsMap.put("productId", map.get("productId"));
        paramsMap.put("mchOrderNo", map.get("mchOrderNo"));
        paramsMap.put("amount", map.get("amount"));
        paramsMap.put("status", map.get("status"));
        paramsMap.put("channelOrderNo", map.get("channelOrderNo"));
        paramsMap.put("channelAttach", map.get("channelAttach"));
        paramsMap.put("param1", map.get("param1"));
        paramsMap.put("param2", map.get("param2"));
        paramsMap.put("paySuccTime", map.get("paySuccTime"));
        paramsMap.put("backType", map.get("backType"));
        paramsMap.put("income", map.get("income"));
        paramsMap.put("reqTime", map.get("reqTime"));
        String sign = PayDigestUtil.getSign(paramsMap, payPassageAccount.getPrivateKey());
        _log.info("加签信息：map-{},retSign->{},sign->{}", map,sign, resSign );
        if(!resSign.equals(sign)){
            return "fail(verify fail)";
        }
        if ("2".equals(map.get("status"))||"3".equals(map.get("status"))) {
            int result = rpcCommonService.rpcPayOrderService.updateStatus4Success(payOrder.getPayOrderId());
             _log.info("更新订单状态为支付成功: result={}", result);
             if(result == 1){
                 // 通知业务系统
                 baseNotify4MchPay.doNotify(payOrder, true);
                 return "success";
            }
        }
        return "fail";
    }
}

